package com.useeinfo.oa.modules.wuHanCommon.base.dao;

import com.useeinfo.framework.extend.dao.CrudDao;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.data.QueryUtils;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.modules.wuHanCommon.base.entity.AutomaticDayData;
import com.useeinfo.oa.modules.wuHanCommon.base.enums.MeterReadingEnum;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("AutomaticDayDataDao")
public class AutomaticDayDataDao implements CrudDao<AutomaticDayData> {
    private final static Logger logger = LoggerFactory.getLogger(AutomaticDayDataDao.class);

    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public Map<String, Object> getSearchCondition(Map<String, String> queryHash) {
        Map<String, Object> conditionHash = new HashMap<>();
        conditionHash.put("deleteStatus = ?{paramIndex} ", false);
        if (queryHash == null || queryHash.size() == 0) {
            return conditionHash;
        }

        String nowTime = queryHash.get("nowTime");
        if (!StringUtils.isEmpty(nowTime)) {
            conditionHash.put("nowTime = ?{paramIndex} ", StringConverters.ToDateOnly(nowTime));
        }
        String checkContent = queryHash.get("checkContent");
        if (!StringUtils.isEmpty(checkContent)) {
            conditionHash.put("automaticModel.checkContent = ?{paramIndex} ", checkContent);
        }
        String endTime = queryHash.get("endTime");
        if (!StringUtils.isEmpty(endTime)) {
            conditionHash.put("automaticModel.endTime = ?{paramIndex} ", StringConverters.ToInteger(endTime));
        }
        String meterReadingEnum = queryHash.get("meterReadingEnum");
        if (!StringUtils.isEmpty(meterReadingEnum)) {
            conditionHash.put("automaticModel.meterReadingEnum = ?{paramIndex} ", MeterReadingEnum.valueOf(meterReadingEnum));
        }

        return conditionHash;
    }

    @Override
    public Long totalRecord(Map<String, String> queryHash) {
        Map<String, Object> conditions = getSearchCondition(queryHash);
        TypedQuery<Long> typedQuery = QueryUtils.getTypedQueryByCondition("select count(a) from AutomaticDayData a ", conditions, entityManager, Long.class);
        return typedQuery.getSingleResult();
    }

    @Override
    public List<AutomaticDayData> findList(QueryParam queryParam) {
        String sqlInfo = queryParam.joinJPQL("select a from AutomaticDayData a ", "order by a.id desc ");
        Map<String, Object> conditions = getSearchCondition(queryParam.getSqlMap());
        TypedQuery<AutomaticDayData> typedQuery = QueryUtils.getTypedQueryByCondition(sqlInfo, conditions, entityManager, AutomaticDayData.class);
        return queryParam.findPageList(typedQuery);
    }

    @Override
    public AutomaticDayData findModel(Long id) {
        return entityManager.find(AutomaticDayData.class, id);
    }

    @Override
    public Integer add(AutomaticDayData model) {
        model.setCreateDate(new Date());
        entityManager.persist(model);
        logger.info("AutomaticDayDataDaoImpl添加AutomaticDayData成功！");
        return 1;
    }

    @Override
    public Integer update(AutomaticDayData model) {
        model.setUpdateDate(new Date());
        entityManager.merge(model);
        return 1;
    }

    @Override
    public Integer delete(Long id) {
        AutomaticDayData automaticDayData = entityManager.find(AutomaticDayData.class, id);
        entityManager.remove(automaticDayData);
        return 1;
    }

    @Override
    public Integer deleteLogic(AutomaticDayData model) {
        return null;
    }
}
